package com.testshiro.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.web.BaseController;
import com.base.util.ObjMapUtil;
import com.base.util.PageUtil;
import com.base.criteria.Page;
import com.base.web.JModelAndView;
import com.base.model.ResultData;
import com.base.model.RetMsg;
import com.testshiro.entity.SysRole;
 import com.testshiro.service.SysRoleService;
/**
 * 
 * <br>
 * <b>功能：</b>SysRoleController<br>
 *   <br>
 */ 
@Controller
@RequestMapping( value = "/data/sysrole" )
public class SysRoleController{
	
	private final static Logger log= Logger.getLogger(SysRoleController.class);
	@Resource
	private SysRoleService sysroleService; 
	
	@RequestMapping( value = "create", method = RequestMethod.POST )
    @ResponseBody
    public RetMsg create( SysRole newObj )throws Exception {
    	RetMsg retMsg = new RetMsg();
    	
        try{
         	sysroleService.add( newObj );
        } catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			retMsg.setCode(0);
			retMsg.setMsg("出错了！请联系管理员");
		}
       
        return retMsg;
    }
    
    @ResponseBody
    @RequestMapping( value = "update", method = RequestMethod.POST )
    public RetMsg update( SysRole updateObj) throws Exception{
	 	RetMsg retMsg = new RetMsg();
	 	
        try{
         	sysroleService.updateBySelective( updateObj );
        } catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			retMsg.setCode(0);
			retMsg.setMsg("出错了！请联系管理员");
		}
       
        return retMsg;
    }
    
    @ResponseBody
    @RequestMapping( value = "delete", method = RequestMethod.POST )
    public RetMsg deleteByid( String id ) throws Exception{
       RetMsg retMsg = new RetMsg();
       
       sysroleService.delete( id );
        return retMsg;
    }
    
 
    @ResponseBody
    @RequestMapping( value = "readById", method = RequestMethod.GET )
    public SysRole readById( Long seqId ) throws Exception {
        SysRole result =sysroleService.queryById( seqId );
        return result;
    }
    
    @ResponseBody
    @RequestMapping( value = "count", method = RequestMethod.GET )
    public Integer count( Integer page,Integer rows,SysRole queryObj ) throws Exception {
		Page pageObj = new Page();
		pageObj.setCurrentPage(page);
	    pageObj.setPageSize(rows);
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    pageObj.setCondition(tmp);
        Integer ret =sysroleService.queryByCount( pageObj );
        return ret;
    }
    
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping( value = "readList", method = RequestMethod.GET )
    public List<SysRole> readList( SysRole queryObj ) throws Exception {
        List<SysRole> ret = null;
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    Page page = new Page();
	    page.clear();
	    page.setCondition(tmp);
	    
        ret =sysroleService.queryByList( page );
        return ret;
    }
    
   
    @ResponseBody
    @RequestMapping( value = "readAndCount", method = RequestMethod.GET )
    public ResultData readAndCount(Integer page,Integer rows,SysRole queryObj) throws Exception {

        List<SysRole> ret = null;
        Page pageObj = new Page();
		pageObj.setCurrentPage(page);
	    pageObj.setPageSize(rows);
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    pageObj.setCondition(tmp);
	    
        ret =sysroleService.queryForPage( pageObj );

        ResultData<SysRole> result = new ResultData<SysRole>();
        result.setRows(ret);
        result.setTotal(pageObj.getTotalCount());
        return result;
    }
    
    @ResponseBody
    @RequestMapping( value = "logicDel", method = RequestMethod.POST )
    public RetMsg logicDel( Integer id ) throws Exception{
       RetMsg retMsg = new RetMsg();
       
   	   SysRole queryObj = new SysRole();
   	   queryObj.setSeqId(id);
   	   queryObj.setIsDel(1);
   	   try{
   	   		sysroleService.updateBySelective( queryObj );
   	   }catch (Exception e1) {
			e1.printStackTrace();
			log.info(e1.getMessage());
			retMsg.setCode(0);
			retMsg.setMsg("出错了！请联系管理员");
			return retMsg;
		}
        
       
        return retMsg;
    }
    
  
	

}
